/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toesp.bean;
 
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; 
import javax.persistence.Query;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import toesp.entity.Mhs;

/**
 *
 * @author satria
 */
public class MhsBean {
    private EntityManagerFactory emf;

    public MhsBean() {
        emf = Persistence.createEntityManagerFactory("swing-researchPU");
    }

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    private void getCloseEntityManager() {
        emf.close();
    }
    
    public boolean add(Mhs mhs) throws IllegalStateException, SystemException{
        boolean bolRet = false;
        Transaction tx = null ;
        
        try { 
            EntityManager em = getEntityManager();
            if(this.getOnebyId(mhs.getMhsNpm())==null){
                em.getTransaction().begin();
                em.persist(mhs);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {  
            tx.rollback(); 
        }finally{
            //this.getCloseEntityManager();
        }
        
        return bolRet; 
    }
    
    public boolean edit(Mhs mhs) throws IllegalStateException, SystemException{
        Transaction tx = null;
        boolean bolRet = false; 
        
        try {
            EntityManager em = getEntityManager();
            if(this.getOnebyId(mhs.getMhsNpm())!=null){
                em.getTransaction().begin();
                em.merge(mhs);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {
            tx.rollback();
        }finally{
            //this.getCloseEntityManager();
        }
        return bolRet;
    }
    
    public boolean del(String p_strNpm) throws IllegalStateException, SystemException{
        boolean bolRet = false;
        Transaction tx = null; 
        
        try {
            EntityManager em = getEntityManager();
            Mhs mhs = this.getOnebyId(p_strNpm);
            if(mhs!=null){
                em.getTransaction().begin();
                em.remove(mhs);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {
            tx.rollback();
        }finally{
            //this.getCloseEntityManager();
        }
        return bolRet;
    }
    
    public List<Mhs> getAll() throws Exception {       
        List<Mhs> lstMhs = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Mhs o ORDER BY o.mhsNpm ASC");   
            lstMhs = q.getResultList();
        } catch (Exception e) {            
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return lstMhs;
    }
    public int count() {
        List lstCount; 
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT COUNT(o) FROM Mhs o");
            lstCount = q.getResultList();
        } finally {
             //this.getCloseEntityManager();
        }
        return ((Long) lstCount.get(0)).intValue();
    }
    
    public List<Mhs> getPaging(int pintStart, int pintMax) throws Exception {
        List<Mhs> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Mhs o ORDER BY o.mhsNama ASC");
            q.setFirstResult(pintStart);
            q.setMaxResults(pintMax);
            lstPg = q.getResultList();
        } finally {
            //this.getCloseEntityManager();
        }
        
        return lstPg;
    }
    
    public List<Mhs> getAllByJur(String pstrJurId) throws Exception {
        List<Mhs> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Mhs o WHERE o.jurusanId=:pjurusanId ORDER BY o.mhsNama ASC");
            q.setParameter("pjurusanId", pstrJurId);
            lstPg = q.getResultList();
        } finally {
            //this.getCloseEntityManager();
        }
        
        return lstPg;
    }
    
    public int countFind(String strFind) {
        List lstCount; 
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT count(o) FROM Mhs o WHERE o.mhsNama LIKE :p_strNama ORDER BY o.mhsNama ASC");
            q.setParameter("p_strNama","%"+strFind+"%");
            lstCount = q.getResultList();
        } finally {
             //this.getCloseEntityManager();
        }
        return ((Long) lstCount.get(0)).intValue();
    }
    
    public List<Mhs> getFindByNamePaging(String strFind,int pintStart, int pintMax) throws Exception {
        List<Mhs> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Mhs o WHERE o.mhsNama LIKE :p_strNama ORDER BY o.mhsNama ASC");
            q.setParameter("p_strNama","%"+strFind+"%");
            q.setFirstResult(pintStart);
            q.setMaxResults(pintMax);
            lstPg = q.getResultList();
        } finally {
            //this.getCloseEntityManager();
        }
        
        return lstPg;
    }
    
    public Mhs getOnebyId(String strNPM) throws Exception {
        Mhs m = null;
        try {              
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Mhs o WHERE o.mhsNpm=:p_mhsNpm");   
            q.setParameter("p_mhsNpm", strNPM);            
            List<Mhs> lstPg = q.getResultList();
            m = (Mhs) lstPg.get(0);
        } catch (Exception e) {            
        }finally{   
            //this.getCloseEntityManager();
        }  
        return m;
    }  
    
    public void commitaj(){
        
    }
}
